import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import 'element-ui/lib/theme-chalk/index.css';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/display.css';
import locale from 'element-ui/lib/locale/lang/zh-CN';

Vue.config.productionTip = false
Vue.use(ElementUI, { locale });

const BASE_URL = 'http://localhost:8080';
window.BASE_URL = BASE_URL;
Vue.prototype.$BASE_URL = BASE_URL;

Vue.prototype.$getUser = () => {
  return localStorage.user ? JSON.parse(localStorage.user) : null;
};

router.beforeEach((to, from, next) => {
  let user = localStorage.user; // 从 localStorage 中获取用户数据
  if (to.path !== '/login' && !user) // 如果目标路径不是登录页且用户不存在
      // 就跳转到登录页,且弹出提示
    next({ path: '/login' }, Vue.prototype.$message.error('请先登录!'));
  else
    next(); // 如果目标路径是登录页,或用户已登录,就直接放行,允许路由跳转
});

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
